package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.Cargo;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.Location2Util;
import com.shaoshaohuo.app.utils.StringUtil;

import java.util.List;

public class CargoListAdapter<T> extends MyBaseAdapter<Cargo> {


    public CargoListAdapter(Context ctx, List<Cargo> list, boolean flag) {
        super(ctx, list, flag);
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup group) {

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_goods, null);
        }

        GoodsViewHolder goodsHolder = GoodsViewHolder.getHolder(convertView);
        Cargo goods = list.get(pos);

        goodsHolder.tv_name.setText(goods.getOrdername());
        if (TextUtils.isEmpty(goods.getShippweight())) {
            goodsHolder.tv_num.setText(goods.getShipparea() + "立方米");
        } else {
            goodsHolder.tv_num.setText(goods.getShippweight() + "千克");
        }
        goodsHolder.tv_start_time.setText(goods.getOrigintime());
        goodsHolder.tv_start_ad.setText(AreaUtil.getAreaFullname(goods.getOrigincityid()) + goods.getOriginaddress());
        goodsHolder.tv_end_ad.setText(AreaUtil.getAreaFullname(goods.getReceivecityid()) + goods.getReceiveaddress());
        goodsHolder.tv_end_time.setText(goods.getReceivetime());
        if (StringUtil.isNumericEmpty(goods.getOriginlat()) || StringUtil.isNumericEmpty(goods.getOriginlng())
                || StringUtil.isNumericEmpty(goods.getReceivelat()) || StringUtil.isNumericEmpty(goods.getReceivelng())) {
            goodsHolder.tv_long.setText("- -");
        } else {
            goodsHolder.tv_long.setText(Location2Util.getInstance(context).distance(goods.getOriginlat(),
                    goods.getOriginlng(), goods.getReceivelat(), goods.getReceivelng()) + "km");
        }
        return convertView;
    }

    static class GoodsViewHolder {

        TextView tv_long;
        TextView tv_end_time;
        TextView tv_end_ad;
        TextView tv_start_ad;
        TextView tv_start_time;
        TextView tv_name;
        TextView tv_num;

        public GoodsViewHolder(View convertView) {
            tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            tv_start_time = (TextView) convertView.findViewById(R.id.tv_start_time);
            tv_start_ad = (TextView) convertView.findViewById(R.id.tv_start_ad);
            tv_end_ad = (TextView) convertView.findViewById(R.id.tv_end_ad);
            tv_end_time = (TextView) convertView.findViewById(R.id.tv_end_time);
            tv_long = (TextView) convertView.findViewById(R.id.tv_long);
            convertView.setTag(this);
        }

        public static GoodsViewHolder getHolder(View convertView) {
            return (GoodsViewHolder) (convertView.getTag() != null ? convertView.getTag() : new GoodsViewHolder(convertView));
        }
    }
}
